package com.hu.video.controller;

import com.hu.video.entity.SocialComment;
import com.hu.video.entity.TUser;
import com.hu.video.service.ISocialCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/api/comment")
public class SocialCommentController {
    @Autowired
    private ISocialCommentService commentService;

    @PostMapping("/add")
    public boolean addComment(@RequestBody SocialComment comment) {
        return commentService.addComment(comment);
    }

    @GetMapping("/list/{videoId}")
    public List<SocialComment> getComments(@PathVariable Long videoId, HttpSession session) {
        // 从session获取当前用户ID
        TUser currentUser = (TUser) session.getAttribute("user");
        Long currentUserId = currentUser != null ? currentUser.getUserId() : 0L;
        
        // 调用新的平铺式查询方法
        return commentService.getFlatCommentListByVideoId(videoId, currentUserId);
    }

    @GetMapping("/replies/{parentId}")
    public List<SocialComment> getReplies(@PathVariable Long parentId, @RequestParam(required = false) Long userId) {
        // 如果userId为null，表示用户未登录，此时isLiked字段将为false
        return commentService.getReplies(parentId, userId);
    }

    @DeleteMapping("/delete/{commentId}")
    public boolean deleteComment(@PathVariable Long commentId) {
        return commentService.deleteComment(commentId);
    }
} 